import axios, { type AxiosResponse } from 'axios'
import { ElMessage } from 'element-plus'
import axiosConfig from './config'

// config

// 创建axios实例
const request = axios.create({
  //一般项目
  baseURL: axiosConfig.BASE_URL, // api 的 base_url  development   production
  timeout: axiosConfig.TIMEOUT, // 请求超时时间
  withCredentials: true
})

request.interceptors.response.use((res: AxiosResponse) => {
  if (res.status !== 200) {
    ElMessage({
      message: `发生错误：${res.data.message}`,
      type: 'error'
    })
  }
  return res
})

const getRequest = (base_url: string, timeout: number) => {
  const req = axios.create({
    baseURL: base_url, // api 的 base_url  development   production
    timeout: timeout // 请求超时时间
  })
  req.interceptors.response.use((res: AxiosResponse) => {
    if (res.status !== 200) {
      ElMessage({
        message: `错误：${res.data.message}`,
        type: 'error'
      })
    }
    return res
  })
  return req
}

export { request }
export { getRequest }
